package com.fishery.customization.controller;

import com.fishery.customization.entity.DragLabel;
import com.fishery.customization.entity.UserLabel;
import com.fishery.customization.entity.vo.SearchLabelCondition;
import com.fishery.customization.service.DragLabelService;
import com.fishery.customization.service.LabelService;
import com.fishery.customization.service.UserLabelService;
import com.fishery.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/customization/user/label")
@Api(tags = "用户自定义标签模块")
@CrossOrigin
public class UserLabelController {


	@Autowired
	private UserLabelService userLabelService;

	@Autowired
	private LabelService labelService;

	@Autowired
	private DragLabelService dragLabelService;

	@PostMapping
	@ApiOperation("查询原来的所有标签")
	public Result searchOriginLabels(@RequestBody SearchLabelCondition condition) {
		if (condition == null
				|| StringUtils.isEmpty(condition.getRole())
				|| StringUtils.isEmpty(condition.getBaseId())) {
			return Result.error("查询失败");
		}
		return Result.success().data(labelService
				.getLabelsJsonByBaseIdAndRole(condition.getBaseId(), condition.getRole()));
	}

	@PutMapping
	@ApiOperation("用户对标签进行自定义")
	public Result AddOrUpdateLabelsOfUser(@RequestBody @ApiParam(value = "UserLabel") UserLabel userLabel) {
		if (userLabel == null
				|| StringUtils.isEmpty(userLabel.getUserId())
				|| userLabel.getCustomizedLabels() == null
				|| userLabel.getCustomizedLabels().isEmpty()) {
			return Result.error("自定义失败");
		}
		return userLabelService.saveOrUpdateLabelsByUserIdAndBaseIdAndRole(userLabel)
				? Result.success("更新成功") : Result.error("更新失败");
	}

	@DeleteMapping("/{userId}")
	@ApiOperation("删除用户对自定义标签（当用户换了一种角色时,需要调用）")
	public Result deleteLabelsOfUser(@PathVariable(name = "userId", required = true)
									 @ApiParam(value = "userId", required = true) String userId) {
		if (StringUtils.isEmpty(userId)) {
			return Result.error("删除失败");
		}
		return userLabelService.removeLabelsByUserId(userId)
				? Result.success("删除成功") : Result.error("删除失败");
	}

	@GetMapping("/sequence/{userId}")
	@ApiOperation("查询用户的拖拽标签的顺序")
	public Result searchSequenceOfLabels(@PathVariable(name = "userId", required = true)
										 @ApiParam(value = "用户id", required = true) String userId) {
		if (StringUtils.isEmpty(userId)) {
			return Result.error("查询失败");
		}
		return Result.success().data(dragLabelService.getLabelSequenceByUserId(userId));
	}

	@PutMapping("/sequence")
	@ApiOperation("保存或更新用户拖拽标签的顺序")
	public Result addOrUpdate(@RequestBody @ApiParam(value = "dragLabel") DragLabel dragLabel) {
		if (dragLabel == null
				|| StringUtils.isEmpty(dragLabel.getUserId())
				|| dragLabel.getLabelsSequence() == null
				|| dragLabel.getLabelsSequence().isEmpty()) {
			return Result.error("操作失败");
		}
		return dragLabelService.saveOrUpdateDragLabelByUserId(dragLabel)
				? Result.success("操作成功") : Result.error("操作失败");
	}

}
